package com.zbw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbw.domain.Book;
import com.zbw.domain.BorrowingBooks;
import com.zbw.domain.BorrowingBooksExample;
import com.zbw.domain.Vo.BookVo;
import com.zbw.mapper.BookCategoryMapper;
import com.zbw.mapper.BookMapper;
import com.zbw.mapper.BorrowingBooksMapper;
import com.zbw.service.IBookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements IBookService {

    @Resource
    private BookMapper bookMapper;
    @Resource
    private BorrowingBooksMapper borrowingBooksMapper;
    @Resource
    private BookCategoryMapper bookCategoryMapper;

    @Override
    public List<BookVo> selectBooksByBookPartInfo(String partInfo) {

        List<BookVo> bookVos = new LinkedList<>();
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("bookName",partInfo);
        List<Map<String, Object>> bookList = bookMapper.selectMaps(queryWrapper);
        if (null == bookList) {
            return bookVos;
        }

        for (Map<String,Object> b : bookList) {
            BookVo bookVo = new BookVo();
//            bookVo.setBookId(b.get("bookId"));
//            bookVo.setBookName(b.getBookName());
//            bookVo.setBookAuthor(b.getBookAuthor());
//            bookVo.setBookPublish(b.getBookPublish());
//            BorrowingBooksExample borrowingBooksExample = new BorrowingBooksExample();
//            BorrowingBooksExample.Criteria criteria1 = borrowingBooksExample.createCriteria();
//            criteria1.andBookIdEqualTo(b.getBookId());
//            List<BorrowingBooks> borrowingBooks = borrowingBooksMapper.selectByExample(borrowingBooksExample);
//
//            if (borrowingBooks == null || borrowingBooks.size() < 1) {
//                bookVo.setIsExist("可借");
//            } else {
//                bookVo.setIsExist("不可借");
//            }
            bookVos.add(bookVo);
        }
        return bookVos;
    }


    @Override
    public List<BookVo> findBooksByCategoryId(int categoryId, int pageNum) {
        Page<Book> objectPage = new Page<>(pageNum, 5);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        String  categoryName= bookCategoryMapper.selectById(categoryId).getCategoryName();
        queryWrapper.eq("bookCategory",categoryName);//根据类目名查询
        bookMapper.selectPage(objectPage,queryWrapper);
        List<Book> bookList=objectPage.getRecords();
        List<BookVo> bookVoList = new LinkedList<>();
//        if (null == bookList) {
////            page.setPageNum(1);
////            page.setPageCount(1);
//            return bookVos;
//        }
        for (Book b : bookList) {
            BookVo bookVo = new BookVo();
            bookVo.setBookId(b.getBookId());
            bookVo.setBookName(b.getBookName());
            bookVo.setBookAuthor(b.getBookAuthor());
            bookVo.setBookPublish(b.getBookPublish());
            BorrowingBooks borrowingBook = borrowingBooksMapper.selectById(b.getBookId());
            if (borrowingBook == null) {
                bookVo.setIsExist("可借");
            } else {
                bookVo.setIsExist("不可借");
            }
            bookVoList.add(bookVo);
        }
//        page.setList(bookVos);
//        page.setPageNum(pageNum);
//        page.setPageSize(10);

        return bookVoList;
    }
}
